package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass=CheckItemService.class) //暴露服务
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean pageBean) {
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        String queryString = pageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);// 3,10
        Page<CheckItem> items = checkItemDao.selectByCondition(queryString);
        long total = items.getTotal();
        List<CheckItem> rows = items.getResult();
        return new PageResult(total,rows);
    }

    @Override
    public void findCheckItemIdByCenterTable(Integer id) {
        if(checkItemDao.findCheckItemIdByCenterTable(id)>0){
           throw new RuntimeException("未删除关联表中数据之前,不允许删除此数据");
        }
        checkItemDao.deleteCheckItemById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);

    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }

    @Override
    public List<CheckItem> findAll() {
       return checkItemDao.findAll();
    }


}
